package com.marktrace.controller;

import com.github.pagehelper.Page;
import com.marktrace.entity.TStudent;
import com.marktrace.service.StudentService;
import com.marktrace.util.ExcelUtil;
import com.marktrace.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/student")
public class StudentController extends BaseController{
    @Autowired
    private StudentService studentService;


    @RequestMapping("/studentinfo")
    public String studentinfo(){
        return "studentinfo.html";
    }

    @ResponseBody
    @RequestMapping("/list")
    public R list(HttpServletRequest request){
        Page page = getPage(request);
        studentService.selectAll();
        return R.list(page);
    }

    @ResponseBody
    @RequestMapping("/add")
    public R add(@RequestBody TStudent tStudent){
        tStudent.setCreatetime(new Date());
        int i = studentService.insert(tStudent);
        if(i>0){
            return R.ok();
        }
        return R.error();
    }

    @ResponseBody
    @RequestMapping("/delete/{id}")
    public R delete(@PathVariable("id") int id){
        int i = studentService.deleteByPrimaryKey(id);
        if(i>0){
            return R.ok();
        }
        return R.error();
    }

    @ResponseBody
    @RequestMapping("/update")
    public R update(@RequestBody TStudent tStudent){
        int i = studentService.updateByPrimaryKey(tStudent);
        if(i>0){
            return R.ok();
        }
        return R.error();
    }

    @ResponseBody
    @RequestMapping("/uploadExcel")
    public R uploadExcel(@RequestParam("file") MultipartFile file){
        List<TStudent> list = ExcelUtil.readExcel("", TStudent.class, file);
        int i = studentService.insertBatch(list);
        if(i>0){
            return R.ok();
        }
        return R.error();
    }

}
